﻿using System;
using System.Collections.Generic;
using System.Globalization;

namespace MapDownloader.Core.Models
{
   /// <summary>
   ///    瓦片坐标
   /// </summary>
   [Serializable]
   public struct TilePoint
   {
      public static readonly TilePoint Empty = new TilePoint();

      public long X { get; }
      public long Y { get; }
      public int Z { get; }
      /// <summary>
      ///    构造方法
      /// </summary>
      /// <param name="x"></param>
      /// <param name="y"></param>
      /// <param name="z"></param>
      public TilePoint(long x, long y,int z)
      {
         X = x;
         Y = y;
         Z = z;
      }

      /// <summary>
      ///    是否是空点
      /// </summary>
      public bool IsEmpty => X == 0 && Y == 0 && Z==0;

      /// <summary>
      ///    重载==
      /// </summary>
      /// <param name="left"></param>
      /// <param name="right"></param>
      /// <returns></returns>
      public static bool operator ==(TilePoint left, TilePoint right)
      {
         return left.X == right.X && left.Y == right.Y && left.Z==right.Z;
      }

      /// <summary>
      ///    重载！=
      /// </summary>
      /// <param name="left"></param>
      /// <param name="right"></param>
      /// <returns></returns>
      public static bool operator !=(TilePoint left, TilePoint right)
      {
         return !(left == right);
      }

      /// <summary>
      ///    重载
      /// </summary>
      /// <param name="obj"></param>
      /// <returns></returns>
      public override bool Equals(object obj)
      {
         if (!(obj is TilePoint))
            return false;
         var comp = (TilePoint) obj;
         return comp.X == X && comp.Y == Y && comp.Z==Z;
      }

      /// <summary>
      ///    重载
      /// </summary>
      /// <returns></returns>
      public override string ToString()
      {
         return $"瓦片坐标为:{{X={X},Y={Y},Z={Z}}}";
      }

      public override int GetHashCode()
      {
         return HashCode.Combine(X, Y,Z);
      }
   }

   /// <summary>
   ///    瓦片比较器
   /// </summary>
   internal class GPointComparer : IEqualityComparer<TilePoint>
   {
      public bool Equals(TilePoint x, TilePoint y)
      {
         return x.X == y.X && x.Y == y.Y && x.Z==y.Z;
      }

      public int GetHashCode(TilePoint obj)
      {
         return obj.GetHashCode();
      }
   }
}